define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");	
	var array = require("$UI/system/lib/base/array");
	var SqliteUtil = require("$UI/demo/sqlite/sqliteUtil");


	var Model = function() {
		this.callParent();
		this.id="";
	};
	
	
	//返回上一页
	Model.prototype.backBtnClick = function(event){
		justep.Shell.closePage();
	};
	
	//减数量
	Model.prototype.reductionBtnClick = function(event){		
		/*
		1、减少数量按钮绑定点击事件onClick()
		2、点击按钮，当前记录的fNumber值减1
		3、fNumber为1时不再相减
		*/
		var row = event.bindingContext.$object;
		var n=row.val("fNumber");
		row.val("fNumber",n-1);
		
	};
	
	//加数量
	Model.prototype.addBtnClick = function(event){
		/*
		1、增加数量按钮绑定点击事件onClick()
		2、点击按钮，当前记录的fNumber值加1
		*/
		var row = event.bindingContext.$object;
		var n=row.val("fNumber");
		row.val("fNumber",n+1);
	};
	
	
	
	
	

	
	//购物车按钮
	Model.prototype.shoppingCartBtnClick = function(event){
		if(localStorage.getItem('LoginState') != " " && localStorage.getItem('LoginState') != undefined){
			justep.Shell.showPage("shop");
		}else{
			justep.Util.hint("请先登录！");	
			justep.Shell.showPage("Login");
		}
	};
	//加入购物车
	Model.prototype.joinCartBtnClick = function(event){
		if(localStorage.getItem('LoginState') != " " && localStorage.getItem('LoginState') != undefined){
			var shopData = this.comp("shopData");
			var fNumber;
			shopData.setFilter("fPhoneFilter","fPhone ='"+ localStorage.getItem('LoginState') +"'");
			shopData.setFilter("fProIDFilter","fProID ='"+ localStorage.getItem('id') +"'");
			shopData.refreshData();
			if(shopData.count() > 0){
				shopData.each(function(param){
					var row = param.row;
					fNumber = row.val('fNumber');
				});
				shopData.setValue("fNumber", fNumber + 1);
				shopData.saveData();
				justep.Util.hint("该商品已存在于购物车，增加购物车中商品的数量！");
			}else{
			shopData.newData({
				"defaultValues" : [ {
					"fPhone" : localStorage.getItem('LoginState'),
					"fProID" : localStorage.getItem('id'),
					"fNumber": 1,
					"fShop" : "待支付",
					"fComment":""
				} ]
			});
			shopData.saveData();
			justep.Util.hint("该商品已成功加入购物车！");	
			}
		}else{
			justep.Util.hint("请先登录！");	
			justep.Shell.showPage("Login");
		}
	};
	
	//购买按钮
	Model.prototype.buyBtnClick = function(event){
		if(localStorage.getItem('LoginState') != " " && localStorage.getItem('LoginState') != undefined){
			justep.Shell.showPage("buy", {
				shopID : localStorage.getItem('id')
			});	
		}else{
			justep.Util.hint("请先登录！");	
			justep.Shell.showPage("Login");
		}
		
	}
	
	//分享按钮
	Model.prototype.shareBtnClick = function(event){
		/*
		1、获取商品ID 
		2、打开分享页面
        3、参数说明 
            thumb：缩略图，可以显示商品图片，注意不能大于32kb，链接从http开始
			webpageUrl: 分享链接，使用location.href显示当前页面，链接从http开始
			scene:分享到微信朋友圈、微信朋友、微信群 
				weixin.Scene.TIMELINE 是分享到微信朋友圈
		 		weixin.Scene.SESSION 是分享给微信朋友、微信群
		
		微信分享代码如下：
		if (!navigator.weixin) {
			return;
		}
		var weixin = navigator.weixin;
		var Utils = require("$UI/system/components/justep/common/utils");		
		weixin.share({
			message : {
				title : this.comp("goodsData").val("fTitle"),
				description : "",
				mediaTagName : "",
				thumb : location.origin + require.toUrl("./detail/img/icon.png"),
				media : {
					type : weixin.Type.WEBPAGE,
					webpageUrl : Utils.getShareUrl()
				}
			},
			scene : weixin.Scene.TIMELINE
		}, function() {
			// alert("Success");
		}, function(reason) {
			// alert("Failed: " + reason);
		});
		*/
	};
	

	Model.prototype.modelParamsReceive = function(event){
		var id;
		if(this.params.ID == undefined)
			id=localStorage.getItem('id');
		else
			id=this.params.ID;
		localStorage.setItem('id', id);
	};
	

	Model.prototype.productDataCustomRefresh = function(event){
		var id=localStorage.getItem('id');
		var Cont = this.getIDByXID('Cont');
		var Price = this.getIDByXID('Price');
		var Sales = this.getIDByXID('Sales');
		var Postage = this.getIDByXID('Postage');
		var Text = this.getIDByXID('Text');
		var Number = this.comp('div4');
		var me = this.comp('contentsImg').getLength();
		var carousel = this.comp("carousel1");
		
		justep.Baas.sendRequest({
			"url" : "/nase/nase_action",
			"action" : "queryNase_product",
			"async" : false,
			"params" : {},
			"success" : function(data) {
				array.each(data.rows, function(obj) {
					var fid = obj.fID.value;
					var fImgUrl = require.toUrl(obj.fImgUrl.value);
					var fCont = obj.fCont.value;
					var fPrice = obj.fPrice.value;
					var fSales = obj.fSales.value;
					var fPostage = obj.fPostage.value;
					var fText = obj.fText.value;
					var fNumber = obj.fNumber.value;
					var i = 0;
					

					if (me > i && id==fid) {
						$(carousel.domNode).find("img").eq(i).attr({
							"src" : fImgUrl,
						});
						document.getElementById(Cont).innerHTML = '<h4 class="text-black cBJNVba">' + fCont + '</h4>';
						document.getElementById(Price).innerHTML = '<span class="text-danger h4 cBJNVba">￥</span><span class="text-danger h4 cBJNVba">' + fPrice + '</span>';
						document.getElementById(Sales).innerHTML = '<span>销售:</span><span>' + fSales + '</span>';
						if(fPostage != "0"){
							document.getElementById(Postage).innerHTML = '<span>邮费:</span><span>' + fPostage + '￥</span>';
						}else{
							document.getElementById(Postage).innerHTML = '<span>包邮</span>';
						}
						if(fText != ""){
							document.getElementById(Text).innerHTML = fText;
						}else{
							document.getElementById(Text).innerHTML = '<h4 style="text-align:center;color:#464646;margin-top:10px;margin-bottom:20px;font-size:16px;">产品详情</h4><h4 style="text-align:center;color:#464646;margin-top:10px;margin-bottom:20px;font-size:16px;">无</h4>';
						}
						
					}
					i = i + 1;

					
					
				})

			}
		});
	};
	
	
	
	

	return Model;
});